package io.yizoo.business.controller;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.yizoo.business.entity.*;
import io.elm.core.CoreController;
import io.elm.core.HMapper;
import io.yizoo.system.entity.sys_org;
import io.yizoo.system.entity.sys_user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/my/business")
public class my_businessController extends CoreController {

    @Autowired
    public io.yizoo.business.mapper.biz_pmis_pjMapper biz_pmis_pjMapper;

    @PostMapping("/GetCustomerByUser")
    public Object GetCustomerByUser(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(biz_crm_customer.class,"t")
                        .Select("t.*,a.name as fzxs_mc")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.fzxs")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .IsWhere("and t.name like concat('%', #{name}, '%')", "name")
                        .Where("and b.id=#{user_id}").IsPage().GetFactor()
        );
        return JsonResultPage(data);
    }

    @PostMapping("/GetAllCustomerByUser")
    public Object GetAllCustomerByUser(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .Select("t.*,a.name as fzxs_mc")
                        .From(biz_crm_customer.class, "t")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.fzxs")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .IsWhere("and t.name like concat('%', #{name}, '%')", "name")
                        .Where("and b.id=#{user_id}")
                        .OrderBy("t.name")
                        .GetFactor());
        return ret;
    }

    @PostMapping("/UpsertPjTask")
    public Object Upsert(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_pmis_pj_task.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeletePjTask")
    public Object DeletePjTask(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_pmis_pj_task.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/UpsertCustomer")
    public Object UpsertCustomer(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_crm_customer.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeleteCustomer")
    public Object DeleteCustomer(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_crm_customer.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/UpsertPj")
    public Object UpsertPj(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_pmis_pj.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeletePj")
    public Object DeletePj(@RequestBody String jsonStr) throws Exception {
        int task = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_pmis_pj_task.class)
                        .Where("and pj_id=#{id}", "id")
                        .GetFactor());
        int ins = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_pmis_pj_ins_stage.class)
                        .Where("and pj_id=#{id}", "id")
                        .GetFactor());
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_pmis_pj.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetInsStageData")
    public Object GetInsStageData(@RequestBody String jsonStr) {
        HMapper.PageData ret = Ctor.HMapper.GetTreePro(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_pmis_pj_ins_stage.class)
                        .Where("and pj_id=#{pj_id}","pj_id")
                        .GetFactor(), "stage_id", "pid"
        );
        if(ret.getSelectData().size()>0)
        {
            return JsonResult(ret);
        }
        else {
            biz_pmis_pjMapper.Initpj_ins_stage(
                    Ctor.DMaker(jsonStr).Selector.GetFactor()
            );
            return JsonResult(
                    Ctor.HMapper.GetTreePro(Ctor.DMaker(jsonStr).Selector
                            .From(biz_pmis_pj_ins_stage.class)
                            .Where("and pj_id=#{pj_id}","pj_id")
                            .GetFactor(), "stage_id", "pid")
            );
        }
    }

    @PostMapping("/UpsertPjAndInsStage")
    public Object UpsertPjAndInsStage(@RequestBody String jsonStr) {
        //更新主表
        ObjectNode node = Ctor.getObjectNode(jsonStr);
        Object ret = Ctor.HMapper.Upsert(biz_pmis_pj.class, node, null);
        //更新子表
        Ctor.HMapper.Save(biz_pmis_pj_ins_stage.class,
                (ArrayNode) node.get("ins_stages"),
                Ctor.DMaker().Deleter
                        .putFactorItem("pj_id",node.get("id").asText())
                        .From(biz_pmis_pj_ins_stage.class)
                        .Where("and pj_id=#{pj_id}","pj_id")
                        .GetFactor());
        return JsonResult(ret != null ? 1 : 0);
    }

    @PostMapping("/GetTree")
    public Object GetTree(@RequestBody String jsonStr) throws Exception {
        Map<String, Object> factor = Ctor.DMaker(jsonStr).Selector
                .From(biz_pmis_pj_task.class)
                .Where("and pj_id = #{pj_id} and (pid IS NULL OR pid = '' OR pid = '0')","pj_id")
                .IsWhere("and name like CONCAT('%', #{name}, '%')","name")
                .IsPage()
                .GetFactor();
        HMapper.PageData data = Ctor.HMapper.GetTree(factor);
        return JsonResult(data);
    }

    @PostMapping("/GetPath")
    public Object GetPath(@RequestBody String jsonStr) {
        Object data = Ctor.HMapper.GetPath(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_pmis_pj_task.class)
                        .Select("id,pid,name")
                        .IsWhere("and id=#{id}","id")
                        .GetFactor());
        return JsonResult(data);
    }

    @PostMapping("/GetLeadByUser")
    public Object GetLeadByUser(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(biz_crm_lead.class,"t")
                        .Select("t.*,a.name as fzxs_mc,ifnull(c.name,t.kh_name) as kh_name_mc")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.fzxs")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .LeftJoin(biz_crm_customer.class,"c","c.id=t.kh_name")
                        .IsWhere("and t.kh_name like concat('%', #{name}, '%')", "name")
                        .Where("and b.id=#{user_id}").IsPage().GetFactor()
        );
        return JsonResultPage(data);
    }

    @PostMapping("/UpsertLead")
    public Object UpsertLead(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_crm_lead.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeleteLead")
    public Object DeleteLead(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_crm_lead.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetOppoByUser")
    public Object GetOppoByUser(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(biz_crm_opportunity.class,"t")
                        .Select("t.*,a.name as fzxs_mc,ifnull(c.name,t.kh_name) as kh_name_mc")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.fzxs")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .LeftJoin(biz_crm_customer.class,"c","c.id=t.kh_name")
                        .IsWhere("and t.name like concat('%', #{name}, '%')", "name")
                        .Where("and b.id=#{user_id}").IsPage().GetFactor()
        );
        return JsonResultPage(data);
    }

    @PostMapping("/UpsertOppo")
    public Object UpsertOppo(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_crm_opportunity.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeleteOppo")
    public Object DeleteOppo(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_crm_opportunity.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetFollowByUser")
    public Object GetFollowByUser(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(biz_crm_follow.class,"t")
                        .Select("t.*,a.name as gjr_mc,ifnull(c.name,t.gjkh) as gjkh_mc")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.gjr")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .LeftJoin(biz_crm_customer.class,"c","c.id=t.gjkh")
                        .IsWhere("and t.gjkh like concat('%', #{name}, '%')", "name")
                        .Where("and b.id=#{user_id}").IsPage().GetFactor()
        );
        return JsonResultPage(data);
    }

    @PostMapping("/UpsertFollow")
    public Object UpsertFollow(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_crm_follow.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeleteFollow")
    public Object DeleteFollow(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_crm_follow.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetAsByUser")
    public Object GetAsByUser(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(biz_crm_after_sales.class,"t")
                        .Select("t.*,a.name as fwr_mc,ifnull(c.name,t.kh_name) as kh_name_mc")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.fwr")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .LeftJoin(biz_crm_customer.class,"c","c.id=t.kh_name")
                        .IsWhere("and t.kh_name like concat('%', #{name}, '%')", "name")
                        .Where("and b.id=#{user_id}").IsPage().GetFactor()
        );
        return JsonResultPage(data);
    }

    @PostMapping("/UpsertAs")
    public Object UpsertAs(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_crm_after_sales.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeleteAs")
    public Object DeleteAs(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_crm_after_sales.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetOrderByUser")
    public Object GetOrderByUser(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(biz_cmis_order.class,"t")
                        .Select("t.*")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.charge_person")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .IsWhere("and t.order_name like concat('%', #{order_name}, '%')", "order_name")
                        .IsWhere("and t.sys_code_key like concat('%', #{order_name}, '%')", "order_name")
                        .Where("and b.id=#{user_id}").IsPage().GetFactor()
        );
        return JsonResultPage(data);
    }

    @PostMapping("/UpsertOrder")
    public Object UpsertOrder(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_cmis_order.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeleteOrder")
    public Object DeleteOrder(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_cmis_order.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetContByUser")
    public Object GetContByUser(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(biz_cmis_contracts.class,"t")
                        .Select("t.*")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.charge_person")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .IsWhere("and t.contract_name like concat('%', #{contract_name}, '%')", "contract_name")
                        .IsWhere("and t.sys_code_key like concat('%', #{contract_name}, '%')", "contract_name")
                        .Where("and b.id=#{user_id}").IsPage().GetFactor()
        );
        return JsonResultPage(data);
    }

    @PostMapping("/UpsertCont")
    public Object UpsertCont(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_cmis_contracts.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeleteCont")
    public Object DeleteCont(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_cmis_contracts.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetPjTree")
    public Object GetPjTree(@RequestBody String jsonStr) throws Exception {
        Map<String, Object> factor = Ctor.DMaker(jsonStr).Selector
                .From(biz_pmis_pj_type.class)
                .IsWhere("and name like CONCAT('%', #{name}, '%')","name")
                .IsPage()
                .GetFactor();
        HMapper.PageData data = Ctor.HMapper.GetTree(factor);
        return JsonResult(data);
    }

    @PostMapping("/GetPjPath")
    public Object GetPjPath(@RequestBody String jsonStr) {
        Object data = Ctor.HMapper.GetPath(
                Ctor.DMaker(jsonStr).Selector
                        .From(biz_pmis_pj_type.class)
                        .Select("id,pid,name")
                        .IsWhere("and id=#{id}","id")
                        .GetFactor());
        return JsonResult(data);
    }

    @PostMapping("/GetEi")
    public Object GetEi() {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker().Selector.From(biz_hrm_ei_basic.class,"t")
                        .Select("t.emp_code as `key`,t.name as value")
                        .Where("and 1=1").GetFactor()
        );
        return JsonResult(data);
    }

    @PostMapping("/GetPjByUser")
    public Object GetPjByUser(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(biz_pmis_pj.class,"t")
                        .Select("t.*,e.name as manager_mc,c.name as type_mc,d.name as org_mc")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a",
                                "a.emp_code=t.manager or a.emp_code=t.saler or a.emp_code=t.booker")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .LeftJoin(biz_pmis_pj_type.class,"c","c.id=t.type")
                        .LeftJoin(sys_org.class,"d","d.id=t.org")
                        .LeftJoin(biz_hrm_ei_basic.class,"e","e.emp_code=t.manager")
                        .IsWhere("and t.name like concat('%', #{name}, '%')", "name")
                        .IsWhere("and t.org =#{org}","org")
                        .IsWhere("and t.manager=#{manager}","manager")
                        .IsWhere("and t.type=#{type}","type")
                        .Where("and b.id=#{user_id}").IsPage().GetFactor()
        );
        return JsonResultPage(data);
    }

    @PostMapping("/GetRecByUser")
    public Object GetRecByUser(@RequestBody String jsonStr) {
        Object data = Ctor.CoreMapper.USelect (
                Ctor.DMaker(jsonStr).Selector.From(biz_bms_receipt.class,"t")
                        .Select("t.*,a.name as sk_person_name")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.sk_person")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .IsWhere("and t.remark like concat('%', #{contract_name}, '%')", "remark")
                        .Where("and b.id=#{user_id}").IsPage().GetFactor()
        );
        return JsonResultPage(data);
    }

    @PostMapping("/UpsertRec")
    public Object UpsertRec(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_bms_receipt.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeleteRec")
    public Object DeleteRec(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_bms_receipt.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetDailyByUser")
    public Object GetDailyByUser(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.*,ifnull(a.name,t.create_user) as create_user_mc,ifnull(b.name,t.pj_code) as pj_mc")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .From(biz_hrm_daily.class,"t")
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.create_user")
                        .LeftJoin(biz_pmis_pj.class,"b","b.sys_code_key=t.pj_code")
                        .LeftJoin(sys_user.class,"c","c.emp_code=a.emp_code")
                        .IsWhere("and t.create_date = #{date}", "date")
                        .Where("and c.id=#{user_id}")
                        .OrderBy("t.create_date desc")
                        .IsPage()
                        .GetFactor());
        return JsonResultPage(ret);
    }

    @PostMapping("/UpsertDaily")
    public Object UpsertDaily(@RequestBody String jsonStr) throws Exception {
        String id = Ctor.HMapper.Upsert(biz_hrm_daily.class, jsonStr);
        return JsonResult(id != null ? 1 : 0);
    }

    @PostMapping("/DeleteDaily")
    public Object DeleteDaily(@RequestBody String jsonStr) throws Exception {
        int rows = Ctor.CoreMapper.UDelete(
                Ctor.DMaker(jsonStr).Deleter
                        .From(biz_hrm_daily.class)
                        .Where("and id=#{id}", "id")
                        .GetFactor());
        return JsonResult(rows);
    }

    @PostMapping("/GetTaskData")
    public Object GetTaskData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.assignee_id as create_user,b.sys_code_key as pj_code,t.remark,t.name as work,ifnull(a.name,t.assignee_id) as create_user_mc,ifnull(b.name,t.pj_id) as pj_mc")
                        .From(biz_pmis_pj_task.class,"t")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.assignee_id")
                        .LeftJoin(biz_pmis_pj.class,"b","b.id=t.pj_id")
                        .LeftJoin(sys_user.class,"c","c.emp_code=a.emp_code")
                        .IsWhere("and (#{date} BETWEEN t.jh_start_time and t.jh_end_time or #{date} BETWEEN t.sj_start_time and t.sj_end_time)", "date")
                        .Where("and c.id=#{user_id}")
                        .OrderBy("t.jh_end_time desc")
                        .GetFactor());
        return JsonResult(ret);
    }

    @PostMapping("/GetRwzpData")
    public Object GetRwzpData(@RequestBody String jsonStr) {
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .Select("t.to_person as create_user,t.remark,t.content as work,ifnull(a.name,t.to_person) as create_user_mc")
                        .From(biz_pmis_rwzp.class,"t")
                        .putFactorItem("user_id",Ctor.getCurrentUserId())
                        .LeftJoin(biz_hrm_ei_basic.class,"a","a.emp_code=t.to_person")
                        .LeftJoin(sys_user.class,"b","b.emp_code=a.emp_code")
                        .IsWhere("and (#{date} BETWEEN t.start_time and t.jh_end_time or #{date} BETWEEN t.start_time and t.sj_end_time)", "date")
                        .Where("and b.id=#{user_id}")
                        .OrderBy("t.jh_end_time desc")
                        .GetFactor());
        return JsonResult(ret);
    }
}

